package com.comall.dc.banktest.infrastructure.dao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.comall.dc.banktest.domain.model.AccountRecord;
import com.comall.dc.banktest.infrastructure.dao.service.AccountRecordDAO;
import com.comall.dc.banktest.infrastructure.entity.AccountRecordPO;
import com.comall.dc.banktest.infrastructure.dao.mapper.IAccountRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 转账记录 服务实现类
 * </p>
 *
 * @author GodMeowIceSun
 * @since 2021-11-02
 */
@Service
public class AccountRecordDAOImpl extends ServiceImpl<IAccountRecordMapper, AccountRecordPO> implements AccountRecordDAO {


    @Override
    public List<AccountRecordPO> listByIdAndTimeRange(
            Integer id, LocalDateTime beginTime, LocalDateTime endTime) {

        return this.list(Wrappers.<AccountRecordPO>lambdaQuery()
                .and(i->i.eq(AccountRecordPO::getFromAccountId, id).or().eq(AccountRecordPO::getToAccountId, id))
                .between(AccountRecordPO::getCreateTime, beginTime, endTime));
    }

    @Override
    public AccountRecord save(AccountRecord accountRecord) {
        AccountRecordPO accountRecordPO = new AccountRecordPO();
        accountRecordPO.setFromAccountId(accountRecord.getFromAccountId());
        accountRecordPO.setToAccountId(accountRecord.getToAccountId());
        accountRecordPO.setCreateTime(accountRecord.getCreateTime());
        accountRecordPO.setValue(accountRecord.getValue());
        accountRecordPO.setOperation(accountRecord.getOperation().getOperation());
        this.save(accountRecordPO);
        return new AccountRecord(accountRecordPO);
    }

}
